<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        div{
            width: 70px;
            height: 30px;
            border-radius: 40px;
            background-color: #ddd;
            position: relative;
        }
        span{
            width: 30px;
            height: 30px;
            background-color: green;
            border-radius: 40px;
            position: absolute;
            transition: .3s;
            left: 0;
        }
    </style>
</head>
<body>

    <div>
        <span></span>
    </div>

    <script>

        var oDiv = document.querySelector('div')
        var oSpan = document.querySelector('span') ;

        var flag = true ;
        oSpan.onclick = function() {
            if(flag) {
                flag = false ;
                this.style.left = '40px' ;
                this.style.background = '#666' ;
            } else {
                flag = true ;
                this.style.left = '0px' ;
                this.style.background = 'green' ;
            }
        }


    </script>
    
</body>
</html>